﻿Public Class RPRICESIN
    Dim connect As CONDB = CONDB.NewConnection()
    Dim PROVIDER_Check As New PROVIDERSCLASS

    Private Sub RIGHTPRICESINGLE_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim objSelectMasproductWithDateAndRight As New RIGHTPRDPRICE
        Dim prdpriceData As DataTable
        prdpriceData = connect.GetTable(objSelectMasproductWithDateAndRight.selectMasproductWithDateAndRight_)
        'masgrpdrgData = connectlocal.GetTable(objSelectDataMasgrpdrg.selectMasgrpdrg_)
        prdpriceSingleGrid.PrimaryGrid.DataSource = prdpriceData

        Dim s5 As FILTERCLASS
        Dim sql5 As String = "SELECT CAST(mtrgtid AS CHAR(4)) AS MTRGTID, mtrgtname, sdet FROM masmtypergt WHERE status = '1'"
        s5 = New FILTERCLASS(MTRGTIDSingleTxt, sql5, "รหัสหมวดคู่สัญญา,ชื่อหมวดคู่สัญญา,มีกลุ่มย่อย", "50,160,50", "1,1,1", "1,1,0")
        's.SetShowInDown = False
        s5.SetShowBorder = True

        STRGTIDSingleTxt.Enabled = False
        SBRGTIDSingleTxt.Enabled = False

        DesignForm(Me)
        DesignGrid(prdpriceSingleGrid)
        DesignGrid(prdpriceSingleHistoryGrid)

        PROVIDER_Check.GETHOSEM(CInt(main.txtUserRequest.Text.Trim), Convert.ToString(Me.Tag))
    End Sub

    Public Sub clearForm()
        PRDNAMELabel.Text = ""
        MTRGTIDSingleTxt.Text = ""
        STRGTIDSingleTxt.Text = ""
        SBRGTIDSingleTxt.Text = ""
        STRGTIDSingleTxt.Enabled = False
        SBRGTIDSingleTxt.Enabled = False
        OPD_PRICETxt.Text = ""
        IPD_PRICETxt.Text = ""
        STDATESingleDate.Text = ""
        prdpriceSingleStatusChk.Checked = False
        historyPRCIDTxt.Text = ""
        PRDCODETxt.Text = ""
        PRCIDTxt.Text = ""
        prdpriceSingleHistoryGrid.PrimaryGrid.DataSource = Nothing
    End Sub

    Private Sub MTRGTIDSingleTxt_Click(sender As Object, e As EventArgs) Handles MTRGTIDSingleTxt.Click
        MTRGTIDSingleTxt.Text = ""
    End Sub

    Private Sub STRGTIDSingleTxt_Click(sender As Object, e As EventArgs) Handles STRGTIDSingleTxt.Click
        STRGTIDSingleTxt.Text = ""
    End Sub

    Private Sub SBRGTIDSingleTxt_Click(sender As Object, e As EventArgs) Handles SBRGTIDSingleTxt.Click
        SBRGTIDSingleTxt.Text = ""
    End Sub

    Private Sub MTRGTIDSingleTxt_TextChanged(sender As Object, e As EventArgs) Handles MTRGTIDSingleTxt.TextChanged
        queryByMTSingle()
    End Sub

    Private Sub STRGTIDSingleTxt_TextChanged(sender As Object, e As EventArgs) Handles STRGTIDSingleTxt.TextChanged
        queryByMTSTSingle()
    End Sub

    Private Sub SBRGTIDSingleTxt_TextChanged(sender As Object, e As EventArgs) Handles SBRGTIDSingleTxt.TextChanged

    End Sub

    Public Sub queryByMTSingle()
        Dim sdetMTRGT As Boolean
        Dim sdetTable As DataTable = connect.GetTable("SELECT SDET FROM masmtypergt WHERE MTRGTID = '" & MTRGTIDSingleTxt.Tag & "'")
        If sdetTable.Rows.Count > 0 Then
            sdetMTRGT = sdetTable.Rows(0)(0).ToString
        End If

        If sdetMTRGT = True Then
            STRGTIDSingleTxt.Enabled = True

            Dim s6 As FILTERCLASS
            Dim sql6 As String = "SELECT CAST(strgtid AS CHAR(4)) AS STRGTID, strgtname, sdet FROM masstypergt WHERE status = '1' AND mtrgtid = '" & MTRGTIDSingleTxt.Tag & "'"
            s6 = New FILTERCLASS(STRGTIDSingleTxt, sql6, "รหัสกลุ่มคู่สัญญา,ชื่อกลุ่มคู่สัญญา,มีกลุ่มย่อย", "50,160,50", "1,1,1", "1,1,0")
            's.SetShowInDown = False
            s6.SetShowBorder = True
        Else
            STRGTIDSingleTxt.Text = ""
            SBRGTIDSingleTxt.Text = ""
            STRGTIDSingleTxt.Enabled = False
            SBRGTIDSingleTxt.Enabled = False
        End If
    End Sub

    Public Sub queryByMTSTSingle()
        Dim sdetSTRGT As Boolean
        Dim sdetTable As DataTable = connect.GetTable("SELECT sdet FROM masstypergt WHERE strgtid = '" & STRGTIDSingleTxt.Tag & "'")
        If sdetTable.Rows.Count > 0 Then
            sdetSTRGT = sdetTable.Rows(0)(0).ToString
        End If

        If sdetSTRGT = True Then
            SBRGTIDSingleTxt.Enabled = True

            Dim s7 As FILTERCLASS
            Dim sql7 As String = "SELECT CAST(sbrgtid AS CHAR(4)) AS SBRGTID, subrgtname FROM massubrgt WHERE status = '1' AND strgtid = '" & STRGTIDSingleTxt.Tag & "'"
            s7 = New FILTERCLASS(SBRGTIDSingleTxt, sql7, "รหัสสิทธิ์,ชื่อสิทธิ์", "50,160", "1,1", "1,1")
            's.SetShowInDown = False
            s7.SetShowBorder = True
        Else
            SBRGTIDSingleTxt.Enabled = False
            SBRGTIDSingleTxt.Text = ""
        End If
    End Sub

    Public Sub queryByMTSTSBSingle()

    End Sub

    Private Sub prdpriceSingleGrid_CellClick(sender As Object, e As DevComponents.DotNetBar.SuperGrid.GridCellClickEventArgs) Handles prdpriceSingleGrid.CellClick
        Dim grid As DevComponents.DotNetBar.SuperGrid.GridRow
        grid = e.GridPanel.ActiveRow

        queryPrdpriceSingleHistory(CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัสผลิตภัณฑ์").Value())

        'PRCIDTxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัสผลิตภัณฑ์").Value()
        PRDCODETxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัสผลิตภัณฑ์").Value()
        PRDNAMELabel.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ชื่อผลิตภัณฑ์").Value()

        addPrdpriceSingleBtn.Enabled = True
    End Sub


    Private Sub prdpriceSingleHistoryGrid_CellClick(sender As Object, e As DevComponents.DotNetBar.SuperGrid.GridCellClickEventArgs) Handles prdpriceSingleHistoryGrid.CellClick
        Dim grid As DevComponents.DotNetBar.SuperGrid.GridRow
        grid = e.GridPanel.ActiveRow
        'f.TXT_CID.Text = grid.Cells.Item(2).Value

        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("วันสิ้นสุด").Value() < DateTime.Now Then
            addPrdpriceSingleBtn.Enabled = False

            Try
                historyPRCIDTxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัสราคา").Value()
            Catch ex As Exception
                historyPRCIDTxt.Clear()
            End Try

            Try
                MTRGTIDSingleTxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ชื่อหมวดคู่สัญญา").Value()
                MTRGTIDSingleTxt.Tag = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัสหมวดคู่สัญญา").Value()
            Catch ex As Exception
                MTRGTIDSingleTxt.Clear()
            End Try

            Try
                STRGTIDSingleTxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ชื่อกลุ่มคู่สัญญา").Value()
                STRGTIDSingleTxt.Tag = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัสกลุ่มคู่สัญญา").Value()
            Catch ex As Exception
                STRGTIDSingleTxt.Clear()
            End Try

            Try
                SBRGTIDSingleTxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("สิทธิ์").Value()
                SBRGTIDSingleTxt.Tag = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัสสิทธิ์").Value()
            Catch ex As Exception
                SBRGTIDSingleTxt.Clear()
            End Try

            Try
                OPD_PRICETxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ส่วนลดOPD(%)").Value()
            Catch ex As Exception
                OPD_PRICETxt.Text = ""
            End Try

            Try
                IPD_PRICETxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ส่วนลดIPD(%)").Value()
            Catch ex As Exception
                OPD_PRICETxt.Text = ""
            End Try


            STDATESingleDate.Value = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("วันเริ่มต้น").Value()

            If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("สถานะการเปิดใช้งาน").Value() = True Then
                prdpriceSingleStatusChk.Checked = True
            Else
                prdpriceSingleStatusChk.Checked = False
            End If
        Else
            Try
                historyPRCIDTxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัสราคา").Value()
            Catch ex As Exception
                historyPRCIDTxt.Clear()
            End Try

            Try
                MTRGTIDSingleTxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ชื่อหมวดคู่สัญญา").Value()
                MTRGTIDSingleTxt.Tag = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัสหมวดคู่สัญญา").Value()
            Catch ex As Exception
                MTRGTIDSingleTxt.Clear()
            End Try

            Try
                STRGTIDSingleTxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ชื่อกลุ่มคู่สัญญา").Value()
                STRGTIDSingleTxt.Tag = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัสกลุ่มคู่สัญญา").Value()
            Catch ex As Exception
                STRGTIDSingleTxt.Clear()
            End Try

            Try
                SBRGTIDSingleTxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("สิทธิ์").Value()
                SBRGTIDSingleTxt.Tag = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัสสิทธิ์").Value()
            Catch ex As Exception
                SBRGTIDSingleTxt.Clear()
            End Try

            Try
                OPD_PRICETxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ส่วนลดOPD(%)").Value()
            Catch ex As Exception
                OPD_PRICETxt.Text = ""
            End Try

            Try
                IPD_PRICETxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ส่วนลดIPD(%)").Value()
            Catch ex As Exception
                OPD_PRICETxt.Text = ""
            End Try


            STDATESingleDate.Value = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("วันเริ่มต้น").Value()

            If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("สถานะการเปิดใช้งาน").Value() = True Then
                prdpriceSingleStatusChk.Checked = True
            Else
                prdpriceSingleStatusChk.Checked = False
            End If

            addPrdpriceSingleBtn.Enabled = True
        End If

        Dim objChkboxUpdate As New CLICKGRIDCHECKBOX
        objChkboxUpdate.clickChkboxUpdate("prdprice", CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("สถานะการเปิดใช้งาน").Value(), "STATUS", "PRCID", historyPRCIDTxt.Text)
    End Sub

    Public Sub queryPrdpriceSingleHistory(ByVal PRDCODE As Integer)
        Dim queryString As String
        queryString = "SELECT a.`prcid` AS 'รหัสราคา', a.`prdcode` AS 'รหัสผลิตภัณฑ์', a.`mtrgtid` AS 'รหัสหมวดคู่สัญญา', b.`mtrgtname` AS 'ชื่อหมวดคู่สัญญา', a.`strgtid` AS 'รหัสกลุ่มคู่สัญญา', c.`strgtname` AS 'ชื่อกลุ่มคู่สัญญา', a.`sbrgtid` AS 'รหัสสิทธิ์', d.`subrgtname` AS 'สิทธิ์', a.`opdprc` AS 'ส่วนลดOPD(%)', a.`ipdprc` AS 'ส่วนลดIPD(%)', a.`stdate` AS 'วันเริ่มต้น', a.`endate` AS 'วันสิ้นสุด', a.`status` AS 'สถานะการเปิดใช้งาน' FROM prdprice a LEFT JOIN masmtypergt b ON a.`mtrgtid` = b.`mtrgtid` LEFT JOIN masstypergt c ON a.`strgtid` = c.`strgtid` LEFT JOIN massubrgt d ON a.`sbrgtid` = d.`sbrgtid` WHERE f_pro = '0' AND prdcode = '" & PRDCODE & "' ORDER BY a.`mtrgtid` ASC, a.`strgtid` ASC, a.`sbrgtid` ASC, a.`stdate` DESC"

        Dim prdpriceSingleHistory As DataTable
        prdpriceSingleHistory = connect.GetTable(queryString)
        prdpriceSingleHistoryGrid.PrimaryGrid.DataSource = prdpriceSingleHistory

        Me.Refresh()

        For i As Integer = 0 To prdpriceSingleHistory.Rows.Count - 1
            Dim dateStart As Date
            Dim dateEnd As Date
            dateStart = prdpriceSingleHistory.Rows(i)("วันเริ่มต้น")
            dateEnd = prdpriceSingleHistory.Rows(i)("วันสิ้นสุด")

            Dim result1 As Integer = DateTime.Compare(dateStart, Now)
            Dim result2 As Integer = DateTime.Compare(dateEnd, Now)

            'Dim result1 As Integer = DateDiff(DateInterval.Day, dateStart, Date.Now)
            'Dim result2 As Integer = DateDiff(DateInterval.Day, dateEnd, Date.Now)

            'MsgBox(result1 & "/" & result2)

            If result1 < 0 And result2 < 0 Then
                'MsgBox("past")
                CType(prdpriceSingleHistoryGrid.PrimaryGrid.Rows(i), DevComponents.DotNetBar.SuperGrid.GridRow).CellStyles.Default.Background.Color1 = Color.FromArgb(179, 179, 179)
            ElseIf result1 <= 0 And result2 >= 0 Then
                'MsgBox("present")
                CType(prdpriceSingleHistoryGrid.PrimaryGrid.Rows(i), DevComponents.DotNetBar.SuperGrid.GridRow).CellStyles.Default.Background.Color1 = Color.FromArgb(171, 217, 93)
            ElseIf result1 > 0 And result2 > 0 Then
                'MsgBox("future")
                CType(prdpriceSingleHistoryGrid.PrimaryGrid.Rows(i), DevComponents.DotNetBar.SuperGrid.GridRow).CellStyles.Default.Background.Color1 = Color.FromArgb(122, 156, 255)
            End If
        Next
    End Sub

    Private Sub addPrdpriceSingleBtn_Click(sender As Object, e As EventArgs) Handles addPrdpriceSingleBtn.Click
        If main.PROVIDERPOLICIES.checklogin_user_ = True Then
            If PROVIDER_Check.PUPD_ = True Then
                If MTRGTIDSingleTxt.Text.ToString.Length > 0 And OPD_PRICETxt.Text.ToString.Length > 0 And IPD_PRICETxt.Text.ToString.Length > 0 Then
                    Dim result As Integer = DateTime.Compare(STDATESingleDate.Value.Date, DateTime.Now.Date)
                    If result > 0 Then
                        Dim objInsertData As New RIGHTPRDPRICE
                        objInsertData.PRDCODE_ = PRDCODETxt.Text
                        objInsertData.MTRGTID_ = CInt(MTRGTIDSingleTxt.Tag)
                        objInsertData.STRGTID_ = CInt(STRGTIDSingleTxt.Tag)
                        objInsertData.SBRGTID_ = CInt(SBRGTIDSingleTxt.Tag)
                        objInsertData.OPDPRC_ = OPD_PRICETxt.Text
                        objInsertData.IPDPRC_ = IPD_PRICETxt.Text
                        objInsertData.STDATE_ = STDATESingleDate.Value.Date
                        objInsertData.ENDATE_ = "9999-09-09"
                        objInsertData.STATUS_ = prdpriceSingleStatusChk.Checked


                        If objInsertData.addPrdprice() = True Then
                            clearForm()
                        End If

                        ''ไม่น่าจะต้อง query ตารางหลักใหม่ ไม่งั้นผู้ใช้จะต้อง sort ข้อมูลบ่อย
                        'queryPrdpriceSingleHistory(PRDCODETxt.Text)
                        'Dim objSelectMasproductWithDateAndRight As New RIGHTPRDPRICE
                        'Dim prdpriceData As DataTable
                        'prdpriceData = connect.GetTable(objSelectMasproductWithDateAndRight.selectMasproductWithDateAndRight_)
                        ''masgrpdrgData = connectlocal.GetTable(objSelectDataMasgrpdrg.selectMasgrpdrg_)
                        'prdpriceSingleGrid.PrimaryGrid.DataSource = prdpriceData


                    Else
                        MsgBox("ต้องระบุวันที่ในอนาคตเท่านั้น")
                    End If
                Else
                    MsgBox("โปรดระบุข้อมูลให้ครบถ้วน")
                End If
            Else
                MsgBox(PROVIDER_Check.PUPDMSG_)
            End If
        Else
            TOASTCLASS.gloadToastMSG(Me, main.PROVIDERPOLICIES.LOGINUSERMSG_)
            main.txtUserRequest.Focus()
        End If
    End Sub
End Class